<template>
  <view class="container">
    <view class="content">
      <view class="content-tab d-rn d-ac d-ja">
        <view class="content-tab-one d-cw d-ac d-jc" v-for="(item, index) in 2" :key="index" @click="handleNav(index)">
          <text :class="navSts == index ? 'active' : 'outactive'">{{
          !index ? $t('shifts') : $t('cycle_shifts')
        }}</text>
          <text v-if="navSts == index"></text>
        </view>
      </view>
      <view class="content-search d-rn d-ac">
        <image src="../../static/img/search.png" mode=""></image>
        <input type="text" v-model="search" :placeholder="$t('search_placeholder')" placeholder-style="#CCCCCC" @confirm="searchList" />
      </view>
    </view>
    <!-- <view class="mains">
			<view class="mains-all">
				<view class="mains-all-title">
					<text>状态</text>
				</view>
				<scroll-view scroll-x="true" scroll-with-animation :scroll-into-view="minSelCrt" :enable-flex='true'
					:show-scrollbar='false' class="scrollview-box">
					<view class="mains-all-one" >
						<text
							@click="handleGourl(item,index)" :id="'minCla'+index"
						 v-for="(item,index) in numList" :key="index" :class="tabNum==index?'actives':'outactives'">{{item.classesName}}</text>
					</view>
				</scroll-view>
			</view>
		</view> -->

    <view class="table" v-if="!navSts">
      <view class="table-head d-rn d-ac d-jb">
        <text class="d-rn d-ac d-jc">{{ $t('shift_name') }}</text>
        <text class="d-rn d-ac d-jc">{{ $t('clockIn_order_workTime') }}</text>
        <text class="d-rn d-ac d-jc">{{ $t('last_modified') }}</text>
        <text class="d-rn d-ac d-jc">{{ $t('operation') }}</text>
      </view>
      <view class="table-body">
        <view class="table-body-list d-rn d-ac d-jb" v-for="(item, index) in numList" :key="index">
          <text class="d-rn d-ac d-jc">{{ item.shiftName }}</text>
          <view class="d-rn d-ac d-jc">
            <text>{{
          `${item.workTimeStart ? item.workTimeStart : "--"}--${item.workTimeEnd ? item.workTimeEnd : "--"
          }`
        }}</text>
          </view>
          <text class="d-rn d-ac d-jc">{{
            item.updateTime || item.createTime
          }}</text>
          <view class="option d-rn d-ac d-jc">
            <template v-if="item.shiftName !== $t('rest')">
              <text @click="handleDetails(item)">{{ $t('details') }}</text>
              <text style="margin: 0 20rpx" @click="handleUpdate(item)">{{ $t('update') }}</text>
              <text @click="handleDel(item)">{{ $t('delete') }}</text>
            </template>
          </view>
        </view>
      </view>
    </view>
    <view class="table" v-else>
      <view class="table-head d-rn d-ac d-jb">
        <text class="d-rn d-ac d-jc">{{ $t('shift_name') }}</text>
        <text class="d-rn d-ac d-jc">{{ $t('clockIn_order_workTime') }}</text>
        <text class="d-rn d-ac d-jc">{{ $t('last_modified') }}</text>
        <text class="d-rn d-ac d-jc">{{ $t('operation') }}</text>
      </view>
      <view class="table-body">
        <view class="table-body-list d-rn d-jb" v-for="(item, index) in numList" :key="index">
          <text class="d-rn d-ac d-js">{{ item.cycleName }}</text>
          <view class="d-cn d-ac d-jc" style="width: 180rpx">
            <template v-if="item.shiftDTOList && item.shiftDTOList.length">
              <view class="d-cn d-jc">
                <text>{{ cycleNameLabel(item) }}</text>
              </view>
            </template>
          </view>
          <text class="d-rn d-ac d-jc">{{
          item.updateTime || item.createTime
        }}</text>
          <view class="option d-rn d-ac d-jc">
            <text @click="handleDetails(item)">{{ $t('details') }}</text>
            <text style="margin: 0 20rpx" @click="handleUpdate(item)">{{ $t('update') }}</text>
            <text @click="handleDel(item)">{{ $t('delete') }}</text>
          </view>
        </view>
      </view>
    </view>

    <view class="footer d-rn d-ac d-jc" @click="handleAddBanCi">
      <text class="d-rn d-ac d-jc">{{ $t('add_new') }}</text>
    </view>


  </view>
</template>

<script>
var _self;
import {
  classesList,
  delclasses,
  cycleAllList,
  delcycle,
} from "../../api/index.js";
export default {

  data() {
    return {
      showModal: true,
      navSts: 0,
      tabNum: 0,
      search: null,
      // 中部分类==选择
      minCla: 0,
      // 底部分类滑动描点
      minSelCrt: null,
      numList: [],
      tableList: [],
    };
  },
  computed: {
    cycleNameLabel() {
      return (item) => {
        if (!item.shiftDTOList.length) return "--";
        let arr = item.shiftDTOList.filter((e) => e);
        let label = arr.map((c) => c.shiftName).toString();
        return label;
      };
    },
  },
  onLoad() {
    _self = this;
  },
  onShow() {
    if (_self.navSts) {
      _self.getcycleList();
    } else {
      _self.getclassesList();
    }
  },
  methods: {
    searchList() {
      if (_self.navSts) {
        _self.getcycleList();
      } else {
        _self.getclassesList();
      }
    },
    getclassesList() {
      classesList({
        shiftName: this.search,
      }).then((res) => {
        if (res.code == 200) {
          _self.numList = res.data;
          //   _self.numList.forEach((item) => {
          //     item.timeRange = JSON.parse(item.timeRange);
          //   });
        }
      });
    },
    getcycleList() {
      cycleAllList({
        cycleName: this.search,
      }).then((res) => {
        if (res.code == 200) {
          _self.numList = res.data;
        }
      });
    },

    handleNav(index) {
      _self.navSts = index;
      if (_self.navSts) {
        _self.getcycleList();
      } else {
        _self.getclassesList();
      }
    },
    handleGourl(val, index) {
      this.tabNum = index;
      this.cate_id = val.id;
      if (index > 0 && index < this.numList.length) {
        this.$nextTick(() => {
          this.minSelCrt = "minCla" + (index - 2);
        });
      }
      this.$emit("changeHeadMonth", this.tabNum);
    },
    handleDetails(item) {
      if (!_self.navSts) {
        item.disabled = true;
        item.titleName = this.$t('view');
        this.$commonFun.goUrl(1, "../addShift/index", item);
      } else {
        item.disabled = true;
        item.titleName = this.$t('edit');
        this.$commonFun.goUrl(1, "../periodicShift/index", item);
      }
    },
    handleUpdate(item) {
      if (!_self.navSts) {
        item.disabled = false;
        item.titleName = this.$t('edit');
        this.$commonFun.goUrl(1, "../addShift/index", item);
      } else {
        item.disabled = false;
        item.titleName = this.$t('edit');
        this.$commonFun.goUrl(1, "../periodicShift/index", item);
      }
    },
    handleDel(item) {
      uni.showModal({
        title: this.$t('prompt'),
        content: this.$t('delete_shift_prompt'),
        success: (mode) => {
          if (mode.cancel) return;
          if (!_self.navSts) {
            delclasses(item.id).then((res) => {
              if (res.code == 200) {
                _self.$commonFun.showTips(res.msg);
                setTimeout(function () {
                  _self.getclassesList();
                }, 2000);
              }
            });
          } else {
            delcycle(item.id).then((res) => {
              if (res.code == 200) {
                _self.$commonFun.showTips(res.msg);
                setTimeout(function () {
                  _self.navSts = 0;
                  _self.getclassesList();
                }, 2000);
              }
            });
          }
        },
      });


    },
    handleAddBanCi() {
      if (!_self.navSts) {
        let item = {
          disabled: false,
          titleName: this.$t('add_new'),
        };
        _self.$commonFun.goUrl(1, "../addShift/index", item);
      } else {
        let item = {
          disabled: false,
          titleName: this.$t('add_new'),
        };
        _self.$commonFun.goUrl(1, "../periodicShift/index", item);
      }
    },
  },
};
</script>

<style lang="scss">
page {
  background-color: #f5f5f5;
}

.container {
  width: 100%;

  .content {
    width: 100%;

    &-tab {
      padding: 24rpx 0;
      box-sizing: border-box;
      position: relative;
      border-bottom: 1px solid #f5f5f5;
      background-color: #ffffff;

      text:nth-of-type(1) {
        font-size: 30rpx;
      }

      text:nth-of-type(2) {
        width: 138rpx;
        height: 4rpx;
        background-color: #fdb911;
        position: absolute;
        top: 93%;
      }

      .active {
        color: #333333;
        font-weight: 500;
      }

      .outactive {
        color: #999999;
      }
    }

    &-search {
      width: 94%;
      padding: 20rpx 35rpx;
      box-sizing: border-box;
      margin: 12rpx auto;
      border-radius: 2em;
      background-color: #ffffff;

      image {
        width: 32rpx;
        height: 32rpx;
      }

      input {
        font-size: 26rpx;
        color: #cccccc;
      }
    }
  }

  .mains {
    display: flex;
    flex-flow: row nowrap;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 30rpx;
    background-color: #ffffff;

    &-all {
      width: 80%;
      display: flex;
      flex-flow: row nowrap;
      align-items: center;
      font-size: 16px;
      text-align: center;
      white-space: nowrap;

      &-one {
        width: 80%;
        display: flex;
        align-items: center;
        flex-flow: row nowrap;
        padding: 0 20rpx;
        box-sizing: border-box;

        text {
          margin-right: 40rpx;
          font-size: 26rpx;
          color: #333333;
        }

        .actives {
          color: red;
        }
      }

      &-title {
        padding: 22rpx 34rpx;
        box-sizing: border-box;

        text {
          font-size: 28rpx;
          color: #333333;
        }
      }
    }
  }

  .table {
    width: 100%;
    padding: 24rpx;
    box-sizing: border-box;
    background-color: #ffffff;

    &-head {
      text {
        font-size: 24rpx;
        font-weight: 500;
        color: #666666;
      }

      text:nth-of-type(1) {
        width: 96rpx;
      }

      text:nth-of-type(2) {
        width: 140rpx;
      }

      text:nth-of-type(3) {
        width: 96rpx;
      }

      text:nth-of-type(4) {
        width: 200rpx;
      }
    }

    &-body {
      margin-top: 30rpx;

      &-list {
        padding-bottom: 23rpx;
        box-sizing: border-box;
        border-bottom: 1px solid #eeeeee;
        margin-bottom: 23rpx;

        text {
          font-size: 24rpx;
          color: #666666;
        }

        text:nth-of-type(1) {
          width: 96rpx;
        }

        text:nth-of-type(2) {
          width: 140rpx;
        }

        text:nth-of-type(3) {
          width: 96rpx;
        }

        .option {
          width: 200rpx;

          text {
            width: unset;
            color: #196bff;
          }
        }
      }
    }
  }

  .footer {
    width: 100%;
    height: 134rpx;
    background-color: #ffffff;
    margin-top: 20rpx;

    text {
      width: 702rpx;
      height: 86rpx;
      border-radius: 2em;
      background-color: #fed52b;
      font-size: 34rpx;
      color: #333333;
    }
  }
}
</style>
